package com.qg.cyapp.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

import com.qg.cyapp.R;
import com.qg.cyapp.component.Dolly;

public class ShiYongFanKaActivity extends Activity {

	private HeadsetPlugReceiver headsetPlugReceiver;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fanka);
		registerHeadsetPlugReceiver();
	}

	private void registerHeadsetPlugReceiver() {
		headsetPlugReceiver = new HeadsetPlugReceiver();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("android.intent.action.HEADSET_PLUG");
		registerReceiver(headsetPlugReceiver, intentFilter);
	}

	@Override
	public void onDestroy() {
		unregisterReceiver(headsetPlugReceiver);
		super.onDestroy();
	}
	
	class HeadsetPlugReceiver extends BroadcastReceiver {

		private static final String TAG = "HeadsetPlugReceiver";

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.hasExtra("state")) {
				if (intent.getIntExtra("state", 0) == 1) {
					Toast.makeText(context, "刷卡成功，订单已提交",
							Toast.LENGTH_LONG).show();
					Dolly.getDolly().destroyDolly();
					ShiYongFanKaActivity.this.finish();
				} else if (intent.getIntExtra("state", 0) == 0) {
					Toast.makeText(context, "刷卡失败，请重试", Toast.LENGTH_LONG)
							.show();
				}
			}

		}

	}
}

